/*
 * Created by JFormDesigner on Wed Dec 16 15:32:03 CST 2020
 */

package cn.fourhundredandeighteen.view.loginView;

import cn.fourhundredandeighteen.UserInfo;
import cn.fourhundredandeighteen.controller.Login;
import cn.fourhundredandeighteen.utill.Result;
import cn.fourhundredandeighteen.view.AdminView.AdminView;
import cn.fourhundredandeighteen.view.Component.BackgroundPanel;
import cn.fourhundredandeighteen.view.Component.Button;
import cn.fourhundredandeighteen.view.StudentView.StudentView;
import cn.fourhundredandeighteen.view.TeacherView.TeacherView;
import cn.fourhundredandeighteen.view.vo.LoginVO;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;


/**
 * @author root
 */
public class LoginView extends JFrame {

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel contentPanel;
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JLabel label3;
    private JPasswordField passwordField1;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JComboBox comboBox1;
    private JButton okButton;
    private JButton cancelButton;
    public LoginView() {
        initComponents();
        comboBox1.addItem("学生");
        comboBox1.addItem("教师");
        comboBox1.addItem("管理员");
        this.setLocation(450, 200);
        this.setMinimumSize(new Dimension(900, 600));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);

        // 得到显示器屏幕的宽高
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        // 定义窗体的宽高
        int windowsWedth = 900;
        int windowsHeight = 600;
        // 设置窗体居中
        this.setBounds((width - windowsWedth) / 2, (height - windowsHeight) / 2, 0, 0);
        this.setMinimumSize(new Dimension(windowsWedth, windowsHeight));

        this.setVisible(true);

    }

    // 取消按钮事件
    private void cancelButtonActionPerformed(ActionEvent e) {
        // TODO add your code here
        System.exit(0);
    }

    @Override
    public void paintComponents(Graphics g) {
        ImageIcon icon = new ImageIcon(getClass().getResource("src/main/resources/Images/login.png"));
        Image img = icon.getImage();
        super.paint(g);
        g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
    }

    // 登录按钮事件
    private void okButtonActionPerformed(ActionEvent e) {
        // TODO add your code here
        Login login = new Login();
        LoginVO loginVO = new LoginVO();
        if (comboBox1.getSelectedItem() == "学生") {  //先判断是否选择登入身份
            if ((textField1.getText() == null) || (passwordField1.getPassword().length == 0)) {
                label4.setText("请先输入账号或密码");
                label4.setVisible(true);
            } else {
                loginVO.setUserId(Long.valueOf(textField1.getText()));
                loginVO.setPassword(String.valueOf(passwordField1.getPassword()));
                Result<String> stringResult = login.studentLogin(loginVO);
                if (stringResult.getCode() == 2000) {
                    System.out.println("登录成功" + stringResult.getData());
                    label4.setText("登录成功" + stringResult.getData());

                    UserInfo.Session = Long.valueOf(textField1.getText());

                    this.setTitle(stringResult.getData());
                    this.dispose();
                    new StudentView(stringResult.getData());
                } else {
                    label4.setText("账号密码错误");
                    label4.setVisible(true);
                }
            }

        } else if (comboBox1.getSelectedItem() == "教师") {
            if ((textField1.getText() == null) || (passwordField1.getPassword().length == 0)) {
                label4.setText("请先输入账号或密码");
                label4.setVisible(true);
            } else {
                loginVO.setUserId(Long.valueOf(textField1.getText()));
                loginVO.setPassword(String.valueOf(passwordField1.getPassword()));
                Result<String> teacherLogin = login.teacherLogin(loginVO);
                if (teacherLogin.getCode() == 2000) {
                    System.out.println("登录成功" + teacherLogin.getData());
                    label4.setText("登录成功" + teacherLogin.getData());

                    UserInfo.Session = Long.valueOf(textField1.getText());

                    this.setTitle(teacherLogin.getData());

                    new TeacherView(teacherLogin.getData());

                    this.dispose();
                } else {
                    label4.setText("账号密码错误");
                    label4.setVisible(true);
                }


            }
        } else if (comboBox1.getSelectedItem() == "管理员") {
            if ((textField1.getText() == null) || (passwordField1.getPassword().length == 0)) {
                label4.setText("请先输入账号或密码");
                label4.setVisible(true);
            } else {
                loginVO.setUserId(Long.valueOf(textField1.getText()));
                loginVO.setPassword(String.valueOf(passwordField1.getPassword()));
                Result<String> adminLogin = login.AdminLogin(loginVO);
                if (adminLogin.getCode() == 2000) {
                    System.out.println("登录成功" + adminLogin.getData());
                    label4.setText("登录成功" + adminLogin.getData());

                    UserInfo.Session = Long.valueOf(textField1.getText());

                    this.setTitle(adminLogin.getData());
                    new AdminView(adminLogin.getData());
                    this.dispose();
                } else {
                    label4.setText("账号密码错误");
                    label4.setVisible(true);
                }


            }
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        Image image = new ImageIcon("src/main/resources/Images/login.png").getImage();
        contentPanel = new BackgroundPanel(image);
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        label3 = new JLabel();
        passwordField1 = new JPasswordField();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        comboBox1 = new JComboBox();
        okButton = new Button();
        cancelButton = new Button(Button.CANCELBUTTON);

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== contentPanel ========
        {
            contentPanel.setBorder(null);
            contentPanel.setLayout(null);

            //---- label1 ----
            label1.setText("\u5b66\u751f\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf");
            label1.setForeground(new Color(32, 34, 42));
            label1.setFont(new Font("Hei", Font.BOLD, 22));
            contentPanel.add(label1);
            label1.setBounds(new Rectangle(new Point(360, 160), label1.getPreferredSize()));

            //---- label2 ----
            label2.setText("\u8d26\u53f7");
            label2.setForeground(new Color(32, 34, 42));
            contentPanel.add(label2);
            label2.setBounds(new Rectangle(new Point(370, 215), label2.getPreferredSize()));

            //---- textField1 ----
            textField1.setText("1");
            contentPanel.add(textField1);
            textField1.setBounds(410, 210, 120, 30);

            //---- label3 ----
            label3.setText("\u5bc6\u7801");
            label3.setForeground(new Color(32, 34, 42));
            contentPanel.add(label3);
            label3.setBounds(new Rectangle(new Point(370, 255), label3.getPreferredSize()));

            //---- passwordField1 ----
            passwordField1.setText("123456");
            contentPanel.add(passwordField1);
            passwordField1.setBounds(410, 250, 120, 30);

            //---- label4 ----
            label4.setText("\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef");
            label4.setForeground(new Color(255, 0, 51));
            label4.setVisible(false);
            contentPanel.add(label4);
            label4.setBounds(370, 335, 160, label4.getPreferredSize().height);
            contentPanel.add(label5);
            label5.setBounds(new Rectangle(new Point(360, 150), label5.getPreferredSize()));

            //---- label6 ----
            label6.setText("\u8eab\u4efd");
            label6.setForeground(new Color(32, 34, 42));
            contentPanel.add(label6);
            label6.setBounds(new Rectangle(new Point(370, 295), label6.getPreferredSize()));

            //---- comboBox1 ----
            comboBox1.setBackground(Color.white);
            contentPanel.add(comboBox1);
            comboBox1.setBounds(410, 290, 90, 30);

            //---- okButton ----
            okButton.setText("\u767b\u5165");
            okButton.addActionListener(e -> okButtonActionPerformed(e));
            contentPanel.add(okButton);
            okButton.setBounds(370, 375, 81, okButton.getPreferredSize().height);

            //---- cancelButton ----
            cancelButton.setText("\u53d6\u6d88");
            cancelButton.addActionListener(e -> cancelButtonActionPerformed(e));
            contentPanel.add(cancelButton);
            cancelButton.setBounds(455, 375, 80, cancelButton.getPreferredSize().height);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for (int i = 0; i < contentPanel.getComponentCount(); i++) {
                    Rectangle bounds = contentPanel.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = contentPanel.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                contentPanel.setMinimumSize(preferredSize);
                contentPanel.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(contentPanel, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
